xquery version "1.0";

(:~
	Its in the session that all data that will be viewed in the
	page are stored. Here are all the functions needed by the
	page for displaying. It includes the table, individual records
	and all formats. This is also where the results from the engine
	are all stored in the HTTP session.
:)

declare namespace MedEx="http://localhost:8080/exist/MedEx";

declare option exist:serialize "method=xml media-type=text/xml omit-xml-declaration=no indent=no";

(:~
	Retrieve the format of the record
:)
declare function MedEx:retrieve-format() as element()*{
	let $format := session:get-attribute("format")
	return <Format> {$format} </Format>
};

(:~
	Retrieve the names of the fields that will be displayed
	in the result table.
:)
declare function MedEx:retrieve-headers() as element()*{
	let $format := session:get-attribute("format")
	return <tag> {for $tag in $format
	where ($tag/display/text() eq "true")
	return $tag}
	</tag>

};

(:~ 
	Retrieve the result table from the HTTP session and
	returns it as an XML data. All the results are sorted
	according to the score
:)
declare function MedEx:retrieve-table() as element()*{
	let $cached := session:get-attribute("cached")
	let $format := session:get-attribute("format")
	return <ResultSet>
			{for $record at $rank in $cached
			where (xs:int($rank) lt 1001)
			return <RECORD><RANK>{$rank}</RANK>
			{$record/*}</RECORD>}
	</ResultSet>

};

(:~ Retrieve a result according to the number given which is the rank of the result :)
declare function MedEx:retrieve-record($num as xs:integer) as element() {
    let $cached := session:get-attribute("cached")
	let $item := 
		if ($cached[$num] instance of node()) then
			util:expand($cached[$num], 'indent=yes')
		else
			$cached[$num]
	return
		<div>
			<div class="pos"><b>RESULT NUMBER: </b>{$num}</div>
			<div class="item">
				{for $tag in $item/*
					return
						<div><b>{$tag/name()}: </b> {if($tag/*) then 
								for $elem in $tag/*/text()
								return <li>{$elem}</li>
								else
										$tag/text()
							}
						</div>
				}
				<br/>
			</div>
		</div>
};

(:~ Take the query results from the engine and store them into the HTTP session. :)
declare function MedEx:store-in-session($results as item()*) as element() {
	let $null := session:set-attribute('cached', $results)
	let $elapsed := request:get-attribute("elapsed")
	return
		<result hits="{count($results)}" elapsed="{if ($elapsed) then $elapsed else 0}"/>
};

(:~ 	
	All the records that are retrieved from the search engine is
	automatically passed to this file. All results are stored in
	the HTTP session. It can be accessed by appending the number
	of the result in "results" to access that record
:)
session:create(),
let $input := request:get-data()
let $results := request:get-attribute("results")
let $pos := xs:integer(request:get-parameter("num", ()))
let $format := xs:integer(request:get-parameter("format",()))
return
    if (string-length($input) gt 0) then
        $input
	else if ($pos eq 0) then
		MedEx:retrieve-table()
	else if ($pos) then
		MedEx:retrieve-record($pos)
	else if ($format eq 0) then
		MedEx:retrieve-format()
	else if ($format eq 1) then
		MedEx:retrieve-headers()
	else
		MedEx:store-in-session($results)